package domain.layer;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import domain.entities.ESub;
import domain.entities.EntitiesEnum;
import domain.entities.EntityFactory;
import domain.entities.IEntity;
import domain.entities.SQLFactory;
import domain.util.HibernateUtil;


public class DataLayer implements IDataLayer {
  
  public DataLayer(){
  }

  private List<IEntity> doQuery(Class<? extends IEntity> entityClass) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    List<IEntity> subsList = session.createQuery(SQLFactory.getSQL(entityClass)).list( );
    return subsList;
  }
  
  public List<IEntity> getEntityList(EntitiesEnum entityType) {
    return doQuery(EntityFactory.getEntityClass(entityType));
  }

  public void addEntity(IEntity entity) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    ESub newSub = new ESub("a cumpara", "kaufen");
    session.createQuery("delete from ESub sub").executeUpdate();
    session.save(newSub);
    session.flush();
    tx.commit();
    session.close();
  }
}
